﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT.BusinessLogic;
using TMDT.BusinessObjects;

namespace TMDT.Web.UI.Controls
{
    public partial class TinhThanhPhoControl : System.Web.UI.UserControl
    {
        public static  Guid IDTinhThanhPho;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            mtvView.ActiveViewIndex = 0;

            txtTenTinhThanhPhoInsert.Text = "";
            var service = new TinhThanhPhoManager();
            rptTinhThanhPho.DataSource = service.GetTinhThanhPho();
            rptTinhThanhPho.DataBind();
        }

        protected void lbnInsert_Click(object sender, EventArgs e)
        {
            var service = new TinhThanhPhoManager();
            var entity = new TinhThanhPho
                             {
                                 IDTinhThanhPho = Guid.NewGuid(),
                                 TenTinhThanhPho = txtTenTinhThanhPhoInsert.Text
                             };
            service.Add(entity);
            LoadData();
        }

        protected void lbnUpdate_Click(object sender, EventArgs e)
        {
            var service = new TinhThanhPhoManager();
            var entity = service.GetTinhThanhPho(IDTinhThanhPho);

            entity.TenTinhThanhPho = txtTenTinhThanhPhoUpdate.Text;

            service.Update(entity);
            LoadData();   
        }

        protected void lbnCancel_Click(object sender, EventArgs e)
        {
            mtvView.ActiveViewIndex = 0;
        }

        protected void ibnDelete_Click(object sender, ImageClickEventArgs e)
        {
            var id = ((ImageButton)sender).CommandArgument;
            var service = new TinhThanhPhoManager();
            service.Remove(id);
            LoadData();
        }

        protected void ibnEdit_Click(object sender, ImageClickEventArgs e)
        {
            var id = ((ImageButton)sender).CommandArgument;
            var service = new TinhThanhPhoManager();
            var entity = service.GetTinhThanhPho(id);

            txtTenTinhThanhPhoUpdate.Text = entity.TenTinhThanhPho;
            IDTinhThanhPho = entity.IDTinhThanhPho;

            mtvView.ActiveViewIndex = 1;
        }
    }
}